<?php

class Login extends CI_controller {

	function index()
	{
		//print_r($this->session->all_userdata());

        $redirect = $this->session->userdata('redirect');
        $data['redirect'] = $redirect; 
		//overskrifter
		//$data['overskrift_left'] = "Login";
		//$data['overskrift_right'] = "Lukket bog!";
		//$data['underskrift_left'] = "";
		//$data['underskrift_right'] = "";

		//indhold i form af views
		$indhold_right[] = 'forside/titelblad';
		if((string) $this->uri->segment(2,0) == "validate_credentials") //er sand, hvis login er mislykkedes
		{
			
			 $indhold_left[] = 'includes/fejl/mislykket_login';
		}
		$indhold_left[] = 'forside/login_form';
		$data['content_left'] = $indhold_left;
		$data['content_right'] = $indhold_right;
		$data['sidefod_right'] = "includes/fejl/tom";
		$data['sidefod_left'] = "includes/fejl/tom";
		
		$this->load->view('includes/templates/bogpaapult',$data);
	}


	function validate_credentials()
	{


		$this->session->unset_userdata('ingame');
		$this->session->unset_userdata('deltager');
		$this->session->unset_userdata('overgrupper');
		$this->session->unset_userdata('admin_element');


		$this->load->model('sessions_model');
		$this->load->model('core/element');

		$query = $this->login_model->validate();
		$username = $this->input->post('username');
		if($query)
		{
			
			$userdata = $this->login_model->get_deltager_data($username);
			
			$data = array(
				'username' => $this->input->post('username'),
				'userid' => $userdata['id'],
				'is_logged_in' => true,		
				'fornavn' => $userdata['navn'],
				'efternavn' => $userdata['efternavn'],
				'side_admin' => $userdata['side_admin']
			);
			
			
			$this->session->set_userdata('deltager',$data);
			

			if($userdata['type_aktuel'] != 0){
				$karakter = new element();
				$karakter->find_alle_over(array($userdata['type_aktuel']), TRUE, array());
				$over = $karakter->alle_over_ider;
				$this->session->set_userdata('overgrupper',$over);
						
			}
			
			$this->login_model->go_ingame($userdata['type_aktuel'],$userdata['kampagne_aktuel']);
			

		}
		else 
		{
			$this->index();
		}
	}

	function skift_blik()
	{

			$id_blik = $this->uri->segment(3,0); // = 0 hvis man er GM
			$id_kampagne = $this->uri->segment(4,0);
			$this->login_model->go_ingame($id_blik, $id_kampagne);
	}

}


?>
